class LoginController < ApplicationController
 
  layout "login" , :except =>["login","temp_user_login"]
  
  def reg
    @user=User.new
  end

  def create
    @user = User.new(params[:user])

    respond_to do |format|
      if @user.save
        flash[:notice] = "用户 #{@user.username} 创建成功. 请登录"
        format.html { redirect_to(:action => "login") }
      else
        format.html { render :action => "reg" }
      end
    end

  end

  def show
    # @user=User.find(params[:id])
    @user=session[:user]
  end 

  def login
    unless cookies
      flash[:error_notice] = "请设置cookie为可用"
    end
    render :layout => false
  end

  def logout
    #user=User.find(params[:id])
    #CurrentUser.destroy_all(:user_id => session[CURRENT_USER_ID])
    session[CURRENT_USER_ID] = nil
    session[CURRENT_TEMPUSER_ID] = nil
    redirect_to("/login/login")
  end
=begin
  def check_login
    user = User.authenticate(params[:username], params[:password])
    if user
      online = false
      ActiveRecord::SessionStore::Session.find(:all).each do |s|
        current_user_id = s.data[CURRENT_USER_ID]
        if session[CURRENT_USER_ID] == current_user_id
          online = true
          break
        end
      end

      if online
puts "-----------------1"
        if session[CURRENT_USER_ID]
puts "-----------------2"
          xml = {:name => user.name, :result => "yes"}
          unless session[CURRENT_USER_ID] == user.id
puts "-----------------3  #{session[CURRENT_USER_ID]}  #{user.id}"
            xml = {:message => "此机器上已经有其他帐户登录", :result => "no"}
          end
        else
puts "-----------------4"
          xml = {:message => "该帐号已经在别处登录", :result => "no"}
        end
      else
puts "-----------------5"
        #CurrentUser.new(:user_id => user.id).save
        session[CURRENT_USER_ID] = nil
        session[CURRENT_USER_ID] = user.id
        xml =  {:name => user.name, :result => "yes"}
      end
    else
      #render :text =>  {:message => "用户名或密码错误", :result => "no"}.to_xml.to_s
      xml = {:message => "用户名或密码错误", :result => "no"}
    end

    #------------------------------------------
    session[CURRENT_USER_ID] = nil
    session[CURRENT_USER_ID] = user.id
    xml =  {:name => user.name, :result => "yes"}
    #-------------------------------------------
    render :xml => xml
  end

  def check_login_backup
    if user = User.authenticate(params[:username], params[:password])
      if CurrentUser.find_by_user_id(user.id)
        if cookies[CURRENT_USER_ID]
          redirect_to :controller => "user/test", :action=>"home"
        else
          flash[:error_notice] = "该帐号已经在别处登录"
          redirect_to :action=>"login"
        end
      else
        CurrentUser.new(:user_id => user.id).save
        cookies[CURRENT_USER_ID] = nil
        cookies[CURRENT_USER_ID] = user.id
        redirect_to :controller => "user/test", :action=>"home"
      end
    else
      flash[:error_notice] = "用户名或密码错误"
      redirect_to :action=>"login"
    end
  end
=end



  def check_login
    if user = User.authenticate(params[:username], params[:password])
        session[CURRENT_USER_ID] = user.id
        redirect_to :controller => "user/test", :action=>"home"
      #end
    else
      flash[:error_notice] = "用户名或密码错误"
      redirect_to :action=>"login"
    end
  end

  def c_admin_login
    session[CURRENT_USER_ID] = nil
    if user = User.authenticate(params[:username], params[:password])
      session[CURRENT_USER_ID] = user.id
      #redirect_to :action=>"show"
        redirect_to("/surveys/prof_surveys")
    else
      flash[:error_notice] = "用户名或密码错误"
      redirect_to :action=>"admin_login"
    end
  end

 def c_admin_login_bak
   if simple_captcha_valid?
    session[CURRENT_USER_ID] = nil
    if user = User.authenticate(params[:username], params[:password])
      session[CURRENT_USER_ID] = user.id
      #redirect_to :action=>"show"
        redirect_to("/surveys/prof_surveys")
    else
      flash[:error_notice] = "用户名或密码错误"
      redirect_to :action=>"admin_login"
    end
   else
      flash[:error_notice] = "验证码错误"
      redirect_to :action=>"admin_login"
   end
  end

  def update
    @user=User.find(params[:id])
  end

  def update_user
    @user = User.find(params[:user][:id])
    @user.is_complete = USER_INFO_COMPLETED
    respond_to do |format|
      if @user.update_attributes(params[:user])
          flash[:notice] = '修改成功.'
          flash[:test_id] = params[:test_id]
          format.html { redirect_to(:controller=> params[:next_controller] , :action => params[:next_action] )}
      else
          format.html
      end
    end
  end

  def home
    render :layout => false
  end
  
  
  def temp_user
    if @user = TempUser.authenticate(params[:username], params[:password]) #密码正确  
      if @user.last_time > Time.now 
        session[CURRENT_TEMPUSER_ID] = nil
        session[CURRENT_TEMPUSER_ID] = @user.id
        redirect_to("/user/temp/welcome/"+@user.id.to_s)
      else
        flash[:error_notice]="该帐号已经过期"
        redirect_to("/login/temp_user_login")
      end
    else
      flash[:error_notice]="帐号或密码错误"
      redirect_to("/login/temp_user_login")
    end
  end
  
  
end
